package com.example.blog.modules.article.service.impl;

import com.example.blog.common.exception.ResourceNotFoundException;
import com.example.blog.modules.article.entity.Article;
import com.example.blog.modules.article.entity.Favorite;
import com.example.blog.modules.article.repository.ArticleRepository;
import com.example.blog.modules.article.repository.FavoriteRepository;
import com.example.blog.modules.article.service.FavoriteService;
import com.example.blog.modules.user.entity.User;
import com.example.blog.modules.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class FavoriteServiceImpl implements FavoriteService {
    private final FavoriteRepository favoriteRepository;
    private final ArticleRepository articleRepository;
    private final UserService userService;

    @Override
    @Transactional
    public void addFavorite(Long articleId, String note) {
        User currentUser = userService.getCurrentUser();
        Article article = articleRepository.findById(articleId)
                .orElseThrow(() -> new ResourceNotFoundException("文章不存在"));

        if (favoriteRepository.existsByUserAndArticle(currentUser, article)) {
            return;
        }

        Favorite favorite = new Favorite();
        favorite.setUser(currentUser);
        favorite.setArticle(article);
        favorite.setNote(note);
        favoriteRepository.save(favorite);
    }

    @Override
    @Transactional
    public void removeFavorite(Long articleId) {
        User currentUser = userService.getCurrentUser();
        Article article = articleRepository.findById(articleId)
                .orElseThrow(() -> new ResourceNotFoundException("文章不存在"));

        favoriteRepository.deleteByUserAndArticle(currentUser, article);
    }

    @Override
    @Transactional(readOnly = true)
    public Page<Favorite> getUserFavorites(Pageable pageable) {
        User currentUser = userService.getCurrentUser();
        return favoriteRepository.findByUser(currentUser, pageable);
    }

    @Override
    @Transactional(readOnly = true)
    public boolean hasFavorited(Long articleId) {
        User currentUser = userService.getCurrentUser();
        Article article = articleRepository.findById(articleId)
                .orElseThrow(() -> new ResourceNotFoundException("文章不存在"));

        return favoriteRepository.existsByUserAndArticle(currentUser, article);
    }

    @Override
    @Transactional(readOnly = true)
    public long countUserFavorites() {
        User currentUser = userService.getCurrentUser();
        return favoriteRepository.countByUser(currentUser);
    }

    @Override
    @Transactional(readOnly = true)
    public long countArticleFavorites(Long articleId) {
        Article article = articleRepository.findById(articleId)
                .orElseThrow(() -> new ResourceNotFoundException("文章不存在"));
        return favoriteRepository.countByArticle(article);
    }
} 